if(!window.F7)
  F7={
     selectionObjs:null,
     isMuti:false,
     instance:null,
     retDatas:[],
     delElem :[]
  };

F7.doOK=function(){
	  if (!F7.isMuti){
	     F7.getSelections();
	  }
  	if (document.all) {
		  dialogArguments.F7Instance.reLoadRecord(F7.retDatas);
		}else{    
			opener.F7Instance.reLoadRecord(F7.retDatas);
		}
	  window.close();
}

F7.doCancel=function(){
  window.close();
}

F7.getSelections=function(){
  var rows=f7grid.getSelections();
  F7.retDatas=[];	             	 
	for (var i = 0; i < rows.length; i++) {
	  var r = rows[i];
	  if (r == null || typeof(r) == typeof(undef)) {
	     continue;
	  }    
	  F7.retDatas.push({value:r.data['id'],text:r.data});   	 
  }
}

F7.mergeRecord = function(type,rows) {
	  if (typeof(rows)=="undefined") return;
		for (var i = 0; i < rows.length; i++) {
	      	var f=false;
	      	for (var j = 0; j < F7.retDatas.length; j++) {
	            if (rows[i].data['id']== F7.retDatas[j].value){
	              f=true;
	              if (type=='minus')
	                F7.retDatas.splice(j,1);
	              j--;
	              break;
	            }
	        }
	        if (type=='plus'&&!f)
	          F7.retDatas.push({value:rows[i].data['id'],text:rows[i].data});
	  };
    F7.updateUI();
		return F7.retDatas;
}

function spanSelect(e){
	 e=e?e:window.event;
   var elm=e.target || e.srcElement;
   if (typeof(sel)!="undefined"&&sel!=null)
			 sel.className='spanCss';
	 elm.className='spanCss spanSel';
	 sel=elm;
}

function spandbClick(e){
   e=e?e:window.event;
   var elm=e.target || e.srcElement;
   F7.mergeRecord('minus',[{data:{id:elm.name}}]);
   sel=null;
}

F7.updateUI = function(){
  var dom = document.getElementById('muti_content');
  if(dom==null)
	  dom=document.getElementById('main:muti_content');
 if (dom == null || typeof(dom)=='undefined')
	 return;
	var n = dom.firstChild;
 	while(n){
 	    var nx = n.nextSibling;
 	    dom.removeChild(n);
 	    n = nx;
 	}
 	
 	for (var i = 0; i < F7.retDatas.length; i++) {                 
     var span = document.createElement('span');
     span.name=F7.retDatas[i].value;
     span.className='spanCss';
     span.innerText=F7.retDatas[i].text['name'];
     dom.appendChild(span);
     span.onclick=spanSelect;
     span.ondblclick=spandbClick;
  }
}

Ext.onReady(function(){
	 f7grid.on('rowdblclick', 
	   function(){
	   	if (!F7.isMuti){
	   	  F7.doOK();
	   	}else
	   		F7.mergeRecord('plus',f7grid.getSelections());
	      //OM.ajax.submit('main',null,{'main:btnOK':''},false);
	   }
	 );
	 if (window.dialogArguments){
	   F7.instance=window.dialogArguments.F7Instance;
	 }else if (window.opener)
	   F7.instance=window.opener.F7Instance;
	 if (F7.instance)  {
	 	  F7.isMuti=F7.instance.muti;
	    f7grid.getSelectionModel().singleSelect=!F7.isMuti;
	 }
	 if (typeof(mutiPanel)=='undefined')
       F7.isMuti=false;

	 if (F7.isMuti){
	 	  if (F7.instance!=null){
	 	    var objs=F7.instance.objectValues;
	 	    for (var i = 0; i < objs.length; i++) {       
	 	      F7.retDatas.push(objs[i].data);
	 	    }
	 	    F7.updateUI();
	 	  }
	    mutiPanel.addTool.apply(mutiPanel, [{id:'plus',
	             handler: function(event, toolEl, panel){
	               F7.mergeRecord('plus',f7grid.getSelections());
	             }
	           },
             {id:'minus',
               handler: function(event, toolEl, panel){
               	if (typeof(sel)=="undefined")  return;
               	  F7.mergeRecord('minus',[{data:{id:sel.name}}]);
               }
             }]);
  }
});